// pages/quality-upload/index.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    az_img: [],
    fileList: [],
    id: "",
  },

  afterRead(event) {
    const image = event.detail.file;

    wx.ajax.uploadFile({
      filePath: image.url,
      callback: (url) => {
        if (url) {
          const { fileList, az_img } = this.data;

          fileList.push(url);
          az_img.push(image);

          this.setData({
            az_img,
            fileList,
          });
        }
      },
    });
  },

  handleDelete(event) {
    const image = event.detail.file;
    const { az_img, fileList } = this.data;
    let index;

    az_img.forEach((item, i) => {
      if (item.url === image.url) {
        index = i;
      }
    });

    az_img.splice(index, 1);
    fileList.splice(index, 1);

    this.setData({
      az_img,
      fileList,
    });
  },

  handelSubmit() {
    const { fileList, id } = this.data;

    console.log(id);
    console.log(fileList);
    if (fileList && fileList.length == 0) {
      wx.showToast({
        title: "请上传照片",
        icon: "none",
        duration: 1000,
      });

      return;
    }

    const params = {
      id,
      az_img: fileList.join(","),
    };

    wx.ajax
      .post("userUplodQualityInfoAZimg.action", params, true)
      .then((res) => {
        if (res.success) {
          wx.showToast({
            title: res.msg,
            icon: "none",
            duration: 1000,
          });

          const timer = setTimeout(() => {
            wx.navigateBack({
              delta: 1,
            });

            clearTimeout(timer);
          }, 1000);
        } else {
          wx.showToast({
            title: res.msg,
            icon: "none",
            duration: 1000,
          });
        }
      });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      id: options.id,
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
});
